$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/sysusercopy/list',
        datatype: "json",
        colModel: [			
			/*{ label: 'userId', name: 'userId', index: 'user_id', width: 50, key: true },*/
			{ label: '法人用户名', name: 'username', index: 'username', width: 80 }, 			
			/*{ label: '密码', name: 'password', index: 'password', width: 80 }, 	*/		
			/*{ label: '盐', name: 'salt', index: 'salt', width: 80 }, */			
			{ label: '邮箱', name: 'email', index: 'email', width: 80 }, 			
			{ label: '手机号', name: 'mobile', index: 'mobile', width: 80 }, 			
			{ label: '状态', name: 'status', width: 60, formatter: function(value, options, row){
				return value === 0 ? 
					'<span class="label label-danger">禁用</span>' : 
					'<span class="label label-success">正常</span>';
			}},	
			 { label: '所属部门', name: 'deptName', sortable: false, width: 75 },	
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 }, 			
			{ label: 'logo', name: 'log', width: 100 ,
				formatter: function ( cellvalue, options, rowObject ){
				
				         return '<img src="'+cellvalue+ '"  style="width:50px;height:50px;" />';
				 }}/*, 			
			{ label: '所属法人', name: 'parent', index: 'parent', width: 80 }, 			
			{ label: '是否为法人  0：否   1：是', name: 'isfr', index: 'isfr', width: 80 }			*/
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
var ztree;
var setting = {
	    data: {
	        simpleData: {
	            enable: true,
	            idKey: "deptId",
	            pIdKey: "parentId",
	            rootPId: -1
	        },
	        key: {
	            url:"nourl"
	        }
	    }
	};
var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
sysUserCopy:{
    status:1,
    deptId:null,
    deptName:null,
    roleIdList:[]
}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.sysUserCopy = {deptName:null, deptId:null, status:1, roleIdList:[]};
			  vm.getDept();
		},
		getDept: function(){
            //加载部门树
            $.get(baseURL + "sys/dept/list", function(r){
                ztree = $.fn.zTree.init($("#deptTree"), setting, r);
                var node = ztree.getNodeByParam("deptId", vm.sysUserCopy.deptId);
                if(node != null){
                    ztree.selectNode(node);
                    vm.sysUserCopy.deptName = node.name;
                }
            })
        },
        deptTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择部门",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                    vm.sysUserCopy.deptId = node[0].deptId;
                    vm.sysUserCopy.deptName = node[0].name;

                    layer.close(index);
                }
            });
        },
		update: function (event) {
			var userId = getSelectedRow();
			if(userId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(userId)
            //vm.sysUserCopy = {deptName:null, deptId:null, status:1, roleIdList:[]};
			  vm.getDept();
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.sysUserCopy.userId == null ? "sys/sysusercopy/save" : "sys/sysusercopy/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.sysUserCopy),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var userIds = getSelectedRows();
			if(userIds == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/sysusercopy/delete",
                        contentType: "application/json",
                        data: JSON.stringify(userIds),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(userId){
			$.get(baseURL + "sys/sysusercopy/info/"+userId, function(r){
                vm.sysUserCopy = r.sysUserCopy;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});